import { he } from "./he";

const { ccclass, property } = cc._decorator;

@ccclass
export class voice extends cc.Component {

    DEFAULT_MUSIC: 0.2
    DEFAULT_EFFECT: 0.5
    DEFAULT_MUSIC_SWITCH: true
    DEFAULT_EFFECT_SWITCH: true
    DEFAULT_SHOCK: true
    shock: true
    checkSetting() {
        let setting = he.Local.getJson("setting", null)
        if (!setting) {
            setting = {
                music_switch: this.DEFAULT_MUSIC_SWITCH,
                effect_switch: this.DEFAULT_EFFECT_SWITCH,
                music: this.DEFAULT_MUSIC,
                effect: this.DEFAULT_EFFECT,
                shock: this.DEFAULT_SHOCK,
                bgm: 'voice/game'
            }
            he.Local.setJson('setting', setting, null)
        }
        this.defaultSetting()
    }
    defaultSetting() {
        let setting = he.Local.getJson('setting', null)
        if (setting.music_switch) {
            cc.audioEngine.setMusicVolume(setting.music)
        } else {
            cc.audioEngine.setMusicVolume(0)
        }
        if (setting.effect_switch) {
            cc.audioEngine.setEffectsVolume(setting.effect)
        } else {
            cc.audioEngine.setEffectsVolume(0)
        }
        this.shock = setting.shock
    }
    playMusic(url, bundle, loop = true) {
        if (!url) {
            url = he.Local.get('bgm', 'setting')
        }
        let clip = he.Load.get(url, cc.AudioClip, bundle)
        if (clip) {
            cc.audioEngine.playMusic(clip, true);
        } else {
            he.Load.getRes(url, cc.AudioClip, (err, clip) => {
                cc.audioEngine.playMusic(clip, loop);
            }, bundle)
        }
    }
    stopMusic(url) {
        cc.audioEngine.stopMusic();
    }
    pauseVoice(status) {
        if (status) {
            cc.audioEngine.pauseMusic()
            cc.audioEngine.setEffectsVolume(0)
        } else {
            let effectVolume = he.Local.get('effect', 'setting')
            cc.audioEngine.resumeMusic()
            cc.audioEngine.setEffectsVolume(effectVolume)
        }
    }
    playEffect(url, bundle) {
        if (!url) {
            console.log("未设置音效路径")
            return null
        }
        let clip = he.Load.get(url, cc.AudioClip, bundle)
        if (clip) {
            return cc.audioEngine.playEffect(clip, false);
        } else {
            return he.Load.getRes(url, cc.AudioClip, function (err, clip) {
                cc.audioEngine.playEffect(clip, false);
            }, bundle)
        }
        return null
    }


}
